package com.abso.mp3tunes.locker.ui.viewers;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import com.abso.mp3tunes.locker.core.LockerServiceException;
import com.abso.mp3tunes.locker.core.data.AbstractLockerData;
import com.abso.mp3tunes.locker.core.services.LockerService;
import com.abso.mp3tunes.locker.ui.ILockerConstants;
import com.abso.mp3tunes.locker.ui.LockerPlugin;
import com.abso.mp3tunes.locker.ui.jobs.GetTreeRootNodesJob;
import com.abso.mp3tunes.locker.ui.views.LockerBrowserView;

/**
 * A specialized tree viewer showing the result of a search operation.
 */
public class SearchViewer extends LockerViewer {

	/** The text field where the user inserts the text to be searched. */
	private Text textField;

	/** The text field where the user inserts the maximum search resulsts. */
	private Text maxResultsField;

	/** The artist checkbox. */
	private Button artistCheckbox;

	/** The album checkbox. */
	private Button albumCheckbox;

	/** The track checkbox. */
	private Button trackCheckbox;

	/**
	 * Constructs a new viewer.
	 * 
	 * @param parent
	 *            the parent control.
	 * @param lockerBrowser
	 *            the Locker browser.
	 * @param textField
	 *            the text field where the user inserts the text to be searched.
	 * @param maxResultsField
	 *            the text field where the user inserts the maximum search resulsts.
	 * @param artistCheckbox
	 *            the artist checkbox.
	 * @param albumCheckbox
	 *            the album checkbox.
	 * @param trackCheckbox
	 *            the track checkbox.
	 */
	public SearchViewer(Composite parent, LockerBrowserView lockerBrowser,
			Text textField, Text maxResultsField, Button artistCheckbox, Button albumCheckbox,
			Button trackCheckbox) {
		super(parent, lockerBrowser);
		setLabelProvider(new SearchViewerLabelProvider());
		this.textField = textField;
		this.maxResultsField = maxResultsField;
		this.artistCheckbox = artistCheckbox;
		this.albumCheckbox = albumCheckbox;
		this.trackCheckbox = trackCheckbox;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.abso.mp3tunes.locker.ui.views.LockerViewer#doUpdateContent()
	 */
	public void doUpdateContent() {
		final String s = textField.getText();
		final int maxSearchResults = NumberUtils.toInt(maxResultsField.getText(), LockerService.DEFAULT_MAX_SEARCH_RESULTS);
		final boolean searchArtists = artistCheckbox.getSelection();
		final boolean searchAlbums = albumCheckbox.getSelection();
		final boolean searchTracks = trackCheckbox.getSelection();
		if (!StringUtils.isEmpty(s)
				&& (searchArtists || searchAlbums || searchTracks)) {
			Job job = new GetTreeRootNodesJob("Search", "Searching...", this) {
				protected Object[] doGetRootNodes()
						throws LockerServiceException {
					AbstractLockerData[] result = LockerPlugin.getDefault()
							.getLockerService().search(s, maxSearchResults, searchArtists,
									searchAlbums, searchTracks);

					/* we mark the result items to better present them in the UI */
					int count = result.length;
					for (int i = 0; i < count; i++) {
						result[i].setData(ILockerConstants.SEARCH_KEY,
								Boolean.TRUE);
					}
					return result;
				}
			};
			job.schedule();
		}
	}

}
